/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.CursorManager;
import org.aswing.graphics.Graphics;
import org.aswing.graphics.Pen;
import org.aswing.graphics.SolidBrush;
import org.aswing.util.EnterFrameImpulser;

import fvaswing.components.cursors.FvAbstractCursor;

/**
 * A 'busy' cursor.
 * 
 * <p>There are 2 ways to use cursor.<br />
 * First, classic instantiation and <strong>AsWing CursorManager</strong> use.
 * {@code
 *   var cursor : FvBusyCursor = new FvBusyCursor();
 *   CursorManager.showCustomCursor( cursor );
 * }
 * 
 * Second, use direct {@link #show} method.
 * {@code
 *    FvBusyCursor.show();
 * }
 * 
 * @author Romain Ecarnot
 */
class fvaswing.components.cursors.FvBusyCursor extends FvAbstractCursor
{
	//-------------------------------------------------------------------------
	// Constants definition
	//-------------------------------------------------------------------------
	
	/** Background color used if {@link #backgroundColor} is not defined. */
	public static var CURSOR_BACKGROUND_COLOR : Number = 0xffffff;
	
	/** Border color used if {@link #borderColor} is not defined. */
	public static var CURSOR_BORDER_COLOR : Number = 0x68808c;
	
	/** Cursor size ( circle diameter ) used if {@link #size} is not defined.*/
	public static var CURSOR_SIZE : Number = 9;
	
		
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _instance : FvBusyCursor;
	
	private var _m : MovieClip;
	private var _h : MovieClip;
	
	private var _impulser : EnterFrameImpulser;
	
	private var _backgroundColor : Number;
	private var _borderColor : Number;
	private var _size : Number;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** Cursor background color. */
	public function get backgroundColor() : Number { return _backgroundColor; }
	public function set backgroundColor ( n : Number ) : Void { _backgroundColor = n;	}
	
	/** Cursor border color. */
	public function get borderColor() : Number { return _borderColor; }
	public function set borderColor ( n : Number ) : Void { _borderColor = n;	}
	
	/** Cursor size ( diameter in pixel ). */
	public function get size() : Number { return _size; }
	public function set size ( n : Number ) : Void { _size = n;	}
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Returns a unique <strong>FvBusyCursor</strong> instance.
	 */
	public static function getInstance() : FvBusyCursor
	{
		if( _instance == undefined ) _instance = new FvBusyCursor();
		return _instance;	
	}
	
	/**
	 * Shows cursor and hides the system cursor. 
	 */
	public static function show() : Void
	{
		CursorManager.showCustomCursor( getInstance() );
		CursorManager.hideSystemCursor();	
	}
	
	/**
	 * Hides busy cursor, pause 'busy' animation and 
	 * shows the default system cursor. 
	 */
	public static function hide() : Void
	{
		getInstance().release();
		CursorManager.hideCustomCursor( getInstance() );
		CursorManager.showSystemCursor();
	}
	
	/**
	 * Constructor.
	 * 
	 * <p>Cursor properties are initialized with constants values.
	 * <ul>
	 *   <li>{@link #CURSOR_BACKGROUND_COLOR}</li>
	 *   <li>{@link #CURSOR_BORDER_COLOR}</li>
	 *   <li>{@link #CURSOR_SIZE}</li>
	 * </ul>
	 */
	public function FvBusyCursor()
	{
		super();
		
		_impulser = new EnterFrameImpulser( 1 );
		_impulser.addActionListener( _update, this );
		
		_backgroundColor = CURSOR_BACKGROUND_COLOR;
		_borderColor = CURSOR_BORDER_COLOR;
		_size = CURSOR_SIZE;
	}	
	
	/**
	 * Paints the image to a specified movieclip at point(x, y)
	 */
	public function paintImage( target : MovieClip, x : Number, y : Number ) : Void
	{
		super.paintImage( target, x, y );
		
		if( !_impulser.isRunning() ) _impulser.start();
	}
	
	/**
	 * Stops the impulser ( rotation manager ).<br />
	 * For example, in critical graphic case, we can hide the busy cursor 
	 * and pause the impulser ( onEnterFrame call ) with {@code release() } 
	 * method.
	 * 
	 * <p>Impulser will be automatically restart when next <strong>paintImage</strong> 
	 * will be called.
	 * 
	 * @see #paintImage
	 */
	public function release() : Void
	{
		_impulser.stop();
	}
	
	/**
	 * Concrete drawing implementation.
	 */
	public function repaint() : Void
	{
		var backBrush : SolidBrush = new SolidBrush( _backgroundColor, 100 );
		var clockBrush : SolidBrush = new SolidBrush( 0x000000, 100 );
		var borderPen : Pen = new Pen( _borderColor, 2, 100 );
		
		_container.removeMovieClip();
		_container = _target.createEmptyMovieClip( "__container__", 1 );
		
		_m = _container.createEmptyMovieClip( "__m__", 2 );
		_h = _container.createEmptyMovieClip( "__h__", 3 );
		
		var fx : Graphics = new Graphics( _container );
		fx.fillCircle( backBrush, 0, 0, _size );
		fx.drawCircle( borderPen, 0, 0, _size );
		
		fx = new Graphics( _m );
		fx.fillRectangle( clockBrush, -0.5, 0, 1, -( _size - 3 ) );
		
		fx = new Graphics( _h );
		fx.fillRectangle( clockBrush, -0.5, -0.5, ( _size - 4 ), 1 );
		
		_applyFilter();
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Visual rotations.
	 */
	private function _update() : Void
	{	
		_m._rotation += 24;
		_h._rotation += 1;
	}
}